package com.hyb.algorithm.data.struct.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * 78. 子集
 * https://leetcode.cn/problems/subsets/
 * @author: ybhu
 * @create: 2023/12/29 18:34
 */
public class Subsets {


    private List<List<Integer>> res;
    public List<List<Integer>> subsets(int[] nums) {

        int n=nums.length;
        res=new ArrayList<List<Integer>>();

        //len 长度逐渐从 1,2,3,....
        for(int i=0;i<=n;i++){
            List<Integer> list=new ArrayList();
            backTrack(nums,i,0,list);
        }

        return res;
    }


    public void backTrack(int[] nums,int len,int start,List<Integer> list){

        //递归终止条件
        if(start==len){
            res.add(new ArrayList(list));
            return;
        }
        //index <len 长度
        for(int index=start;index<len;index++){
            list.add(nums[index]);

            //新的start = index+1
            backTrack(nums,len,index+1,list);

            //回溯
            list.remove(list.size()-1);
        }

    }
}
